package com.job.common.result;


import lombok.Data;

/**
 * 全局统一返回结果类
 */
@Data
public class Result<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    public Result() {
    }

    private static <T> Result<T> build(T data) {
        Result<T> result = new Result<>();
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    /**
     * 构建一个表示成功的结果对象
     *
     * @param data 结果数据，可以是任意类型
     * @param <T>  数据的类型
     * @return 返回一个包含成功状态和数据的结果对象
     */
    public static <T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok() {
        return Result.ok(null);
    }

    public static <T> Result<T> fail() {
        return build(null, ResultCodeEnum.FAIL);
    }

    public static <T> Result<T> fail(T data) {
        return build(data, ResultCodeEnum.FAIL);
    }

    public static <T> Result<T> fail(ResultCodeEnum resultCodeEnum) {
        return build(null, resultCodeEnum);
    }
    public static <T> Result<T> fail(ResultCodeEnum resultCodeEnum, T data) {
        return build(data, resultCodeEnum);
    }
    public static <T> Result<T> fail(Integer code, String message) {
        Result<T> result = build(null);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> Result<T> validateFailed() {
        return fail(ResultCodeEnum.PARAM_ERROR);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param msg 提示信息
     */
    public static <T> Result<T> validateFailed(String msg) {
        return fail(ResultCodeEnum.RC400.getCode(), msg);
    }


    /**
     * 未登录返回结果
     */
    public static <T> Result<T> unauthorized(T data) {
        return build( data,ResultCodeEnum.ADMIN_LOGIN_AUTH);
    }

    /**
     * 未授权返回结果
     */
    public static <T> Result<T> forbidden(T data) {
        return build( data,ResultCodeEnum.ADMIN_ACCESS_FORBIDDEN);
    }
}

